/**
 * 导入结果包装类
 */
package com.mavenq.fly.utils.excelv4.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ImportResult<T> {
    private final List<T> successData;
    private final List<ImportErrorDetail> errors;
    private final int totalRows;
    private final int successCount;
    private final int errorCount;

    public ImportResult(List<T> successData, List<ImportErrorDetail> errors, 
                       int totalRows, int successCount, int errorCount) {
        this.successData = successData != null ? successData : new ArrayList<>();
        this.errors = errors != null ? errors : new ArrayList<>();
        this.totalRows = totalRows;
        this.successCount = successCount;
        this.errorCount = errorCount;
    }

    // Getter 方法
    public List<T> getSuccessData() { return successData; }
    public List<ImportErrorDetail> getErrors() { return errors; }
    public int getTotalRows() { return totalRows; }
    public int getSuccessCount() { return successCount; }
    public int getErrorCount() { return errorCount; }

    /**
     * 导入是否完全成功
     */
    public boolean isSuccess() {
        return errorCount == 0;
    }

    /**
     * 获取错误摘要
     */
    public String getErrorSummary() {
        if (errors.isEmpty()) {
            return "导入成功，无错误";
        }
        
        Map<ImportErrorDetail.ErrorType, Long> errorTypeCount = errors.stream()
            .collect(Collectors.groupingBy(ImportErrorDetail::getErrorType, Collectors.counting()));
        
        StringBuilder summary = new StringBuilder();
        summary.append(String.format("导入完成: 成功 %d 条, 失败 %d 条, 总计 %d 行\n", 
                                   successCount, errorCount, totalRows));
        summary.append("错误类型统计:\n");
        errorTypeCount.forEach((type, count) -> {
            summary.append(String.format("  %s: %d 个错误\n", type, count));
        });
        
        return summary.toString();
    }
}